﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.IO;

namespace MyPublisher.Composition.Fop
{
    public class DitaComposition : IComposition
    {
        public void Composite(Fragments inputs, Fragment output)
        {
            try
            {
                XDocument document = new XDocument(new XDeclaration("1.0", "utf-8", null));
                XElement map = new XElement("map", new XAttribute("title", "MyPublisher"));
                XDocumentType documentType = new XDocumentType("map", "-//OASIS//DTD DITA Map//EN", "../dtd/map.dtd",null);
                document.Add(documentType, map);
                foreach (Fragment input in inputs)
                {
                    FileInfo fi = new FileInfo(input.Path);
                    map.Add(new XElement("topicref",
                        new XAttribute("href", fi.Name)));
                }

                document.Save(output.Path);
            }
            catch (Exception e)
            {
                throw new CompositionException("Failed to composite.",e);
            }
        }
    }
}
